<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"/>
    <title>组件的状态-装载、卸载</title>
</head>
<body>
<div id="app"></div>

<!--script-->
<script src="lib/react/react.js"></script>
<script src="lib/react/JSXTransformer.js"></script>
<script type="text/jsx">
    var MessageBox = React.createClass({
        getInitialState: function () {
            console.log('getInitialState')
            return {
                count: 0
            }
        },
        getDefaultProps: function () {
            console.log('getDefaultProps');
            return {};
        },
        componentWillMount: function () {
            console.log("componentWillMount");
            var self = this;
            this.timer = setInterval(function () {
                self.setState({
                    count: self.state.count + 1
                });
            }, 1000);
        },
        componentDidMount: function () {
            console.log("componentDidMount");
            console.log(this.getDOMNode());
        },
        componentWillUnmount: function () {
            console.log("componentWillUnmount:you are trying to kill me");
            clearInterval(this.timer);
        },
        killMyself: function () {
            React.unmountComponentAtNode(document.getElementById("app"));
        },
        render: function () {
            console.log("render");
            return (
                <div>
                    <h1>计数:{this.state.count}</h1>
                    <button onClick={this.killMyself}>卸载掉这个组件</button>
                    <SubMessage/>
                </div>
            );
        }
    });

    var SubMessage = React.createClass({
        render: function () {
            return (
                <div>
                    <h3>写代码很有意思</h3>
                    <Footer/>
                </div>
            )
        }
    });

    var Footer = React.createClass({
        render: function () {
            return (
                <div>
                    <small>因为我在用代码创造</small>
                </div>
            )
        }
    })


    var messageBox = React.render(<MessageBox/>,
        document.getElementById("app"),
        function () {
            console.log("渲染完成了")
        });
</script>
</body>
</html>
